package edu.washington.ling.syntric.io;

import java.util.ArrayList;

import edu.washington.ling.syntric.struct.tree.Token;
import edu.washington.ling.syntric.struct.tree.Tree;
import edu.washington.ling.syntric.util.Utils;


public class TreeWriterLinks extends TreeWriter {
	private String writeWords(Tree tree){
		ArrayList<String> words = new ArrayList<String>();
		for (Token t: tree.getTokenList()){
			words.add(t.getWord());
		}
		String wordStr = Utils.join(words, " ");
		return String.format("sent: %s", wordStr);
	}
	private String writeTags(Tree tree){
		ArrayList<String> tags = new ArrayList<String>();
		for (Token t: tree.getTokenList()){
			tags.add(t.getTag());
		}
		String tagStr = Utils.join(tags, " ");
		return String.format("tags: %s", tagStr);
	}
	private String writeInfo(Tree tree){
		int sentLen = tree.getTokenList().size();
		String rootName = tree.getDSRoot().getName();
		return String.format("sent_leng=%d root_idx=%s", sentLen, rootName);
	}
	private String writeDSLink(Token t){
		ArrayList<String> linkStrs = new ArrayList<String>();
		linkStrs.add(t.getName());
		if (t.hasDSHead()) {
			linkStrs.add(t.getDSHead().getName());
		} else {
			linkStrs.add(t.getName());
		}
		
		if (t.hasDSRelation()){
			linkStrs.add(t.getDSRelation());
		}
		linkStrs.add("#");
		linkStrs.add(t.getWord());
		if (t.hasDSHead()) {
			linkStrs.add(t.getDSHead().getWord());
		} else {
			linkStrs.add(t.getWord());
		}
		return Utils.join(linkStrs, " ");
		
	}
	@Override
	public TreeRepresentation writeTree(Tree tree){
		ArrayList<String> lines = new ArrayList<String>();
		lines.add(writeWords(tree));
		lines.add(writeTags(tree));
		lines.add(writeInfo(tree));
		for (Token t: tree.getTokenList()) {
			lines.add(writeDSLink(t));
		}
		return (new TreeRepresentation(lines, tree.getLayer(), "links"));
	}
}
